<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>GET/POST</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        img{max-width: 650px;}
    </style>
</head>
<body class="container">
<h1>GET/POST</h1>
<h2>GET请求，获取url内容</h2>
<div class="well">
    <h3>创建一个接收get请求的服务</h3>
    <xmp>
//获取GET请求内容
var http=require('http');
var util=require('util');
var url=require('url');
http.createServer(function (req,res) {
res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
res.end(util.inspect(url.parse(req.url,true)));
}).listen(3000);
    </xmp>
    <h3>发送一个get请求</h3>
    <xmp>
http://localhost:3000/?url=http://www.runoob.com&node=菜鸟
    </xmp>
    <h3>请求的输出结果：</h3>
    <xmp>
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?url=http://www.runoob.com&node=%E8%8F%9C%E9%B8%9F',
query: { url: 'http://www.runoob.com', node: '菜鸟' },
pathname: '/',
path: '/?url=http://www.runoob.com&node=%E8%8F%9C%E9%B8%9F',
href: '/?url=http://www.runoob.com&node=%E8%8F%9C%E9%B8%9F' }
    </xmp>
</div>
<h2>获取get请求的参数</h2>
<div class="well">
    <h3>创建一个解析get请求url参数的服务</h3>
    <xmp>
/**
* http://localhost:3000/?url=http://www.runoob.com&node=%E8%8F%9C%E9%B8%9F
*/
var http=require('http');
var url=require('url');
var util=require('util');
http.createServer(function (req,res) {
res.writeHead(200,{"Content-Type":"text/plain;charset=utf-8"});
var parms=url.parse(req.url,true).query;
res.write('网站名称：'+parms.name);
res.write('\n');
res.write('网站URL：'+parms.url);
res.end();
}).listen(3000);
    </xmp>
</div>
<h2>post</h2>
<div class="well">
    <xmp>
        /**
        * Created by zhaott on 2017/11/16.
        */
        var http = require('http');
        var querystring = require('querystring');

        var postHTML =
        '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
    '<body>' +
    '<form method="post">' +
        '网站名： <input name="name"><br>' +
        '网站 URL： <input name="url"><br>' +
        '<input type="submit">' +
        '</form>' +
    '</body></html>';

        http.createServer(function (req, res) {
        var body = "";
        req.on('data', function (chunk) {
        body += chunk;
        });
        req.on('end', function () {
        // 解析参数
        body = querystring.parse(body);
        // 设置响应头部信息及编码
        res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});

        if(body.name && body.url) { // 输出提交的数据
        res.write("网站名：" + body.name);
        res.write("<br>");
        res.write("网站 URL：" + body.url);
        } else {  // 输出表单
        res.write(postHTML);
        }
        res.end();
        });
        }).listen(3000);
    </xmp>
</div>
</body>
</html>